package io.openkunlun.examples.springboot.consumer;

import com.google.common.collect.Lists;
import io.openkunlun.examples.springboot.api.RpcRequest;
import io.openkunlun.examples.springboot.api.RpcResponse;
import io.openkunlun.examples.springboot.api.RpcService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class RpcController {

    @Autowired
    private RpcService rpcService;

    private final static Logger logger = LoggerFactory.getLogger(RpcService.class);

    @GetMapping("/")
    public String index() {
        try {
            RpcResponse rpcResponse = rpcService.rpc(new RpcRequest(String.valueOf(System.currentTimeMillis())));
            logger.info(String.format("方法rpc调用成功, 返回值:%s", rpcResponse));
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            RpcResponse rpcResponse = rpcService.list(Lists.newArrayList(new RpcRequest(String.valueOf(System.currentTimeMillis()))));
            logger.info(String.format("方法list调用成功, 返回值:%s", rpcResponse));
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            rpcService.multi("张三", 33);
            logger.info(String.format("方法multi调用成功"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            Map<String, List<RpcRequest>> request = new HashMap<>();
            request.put("1", Lists.newArrayList(new RpcRequest(String.valueOf(System.currentTimeMillis()))));
            List<RpcResponse> mapRes = rpcService.map(request);
            logger.info(String.format("方法map调用成功, 返回值:%s", mapRes));
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            rpcService.error();
        } catch (Exception e) {
            e.printStackTrace();
        }

//        try {
//            rpcService2.multi("李四", 22);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        return "success!" + System.currentTimeMillis();
    }
}
